package com.github_dididipapa.mall.product.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @description: Redisson 配置类
 * @author: dididi_papa
 * @email: mitis1624087624@gmail.com
 * @date: 2025/08/01  18:36
 */
@RefreshScope
@Configuration
public class RedissonConfig {
    @Value("${spring.redis.connectionType}")
    private String connectionType;
    @Value("${spring.redis.host}")
    private String redisHost;
    @Value("${spring.redis.port}")
    private String redisPort;
    @Value("${spring.redis.username}")
    private String redisUsername;
    @Value("${spring.redis.password}")
    private String redisPassword;

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() throws IOException {
        Config config = new Config();
        config.useSingleServer()
                .setAddress(connectionType + "://" + redisHost + ":" + redisPort)
                .setUsername(redisUsername)
                .setPassword(redisPassword);
        return Redisson.create(config);
    }
}
